{% extends "project_base_en.html" %}
{% block title %}Transaktionen{% endblock %}
{% block script %}
	function transaction_SubmitClick(event)
	{
		$('teditform').addClassName("hidden");
		new Ajax.Request("/transactions", {   
			method: 'post',
			parameters: {
				action: 'update', 
				project: project_id, 
				transaction: $F('transaction_key'),
				date: $F('transaction_date'),
				source: $F('transaction_source'),					
				dest: $F('transaction_dest'),
				ammount: $F('transaction_ammount'),
				check: $F('transaction_check')!=null?"True":"False",
				currency: $F('transaction_currency'),
				text: $F('transaction_text')
			},
			onSuccess: function(transport) 
			{ 
				$('transactions').update(transport.responseText);
				$$('tr.transaction').invoke('observe', 'click', transactionClick);
			},
			onFailure: function(transport)
			{
				$('teditform').removeClassName("hidden");
				$('transaction_edit_error').update(transport.responseText);
				$('transaction_edit_error').removeClassName("hidden");
			}
		}); 
		return false;
	}
	
	function transaction_CloseClick(event) {
		$('teditform').addClassName("hidden");
		return false;
	}
	
	function transaction_DeleteClick(event)
	{
		$('teditform').addClassName("hidden");
		new Ajax.Request("/transactions", {   
			method: 'post',
			parameters: {
				action: 'delete', 
				project: project_id, 
				transaction: $F('transaction_key')
			},
			onSuccess: function(transport) 
			{ 
				$('transactions').update(transport.responseText);
				$$('tr.transaction').invoke('observe', 'click', transactionClick);
			},
			onFailure: function(transport)
			{
				alert(transport.responseText);
			}
		}); 
		return false;
	}
	
  	function transactionClick(event)
  	{
		var row = Event.element(event).parentNode;
		var id = row.id;
		var data = row.descendants();
		var form = $('teditform');
		$('transaction_edit_error').addClassName("hidden");
		form.removeClassName("hidden");
		$('transaction_delete').removeClassName("hidden");
		$('transaction_update').innerHTML = "Aktualisieren";
		$('transaction_key').setValue(id);
		$('transaction_date').setValue(data[0].innerHTML);
		$('transaction_source').setValue(data[1].getAttribute('value'));
		$('transaction_dest').setValue(data[2].getAttribute('value'));
		$('transaction_ammount').setValue(data[3].innerHTML);
		$('transaction_currency').setValue(data[4].getAttribute('value'));
		$('transaction_text').setValue(data[5].innerHTML);
		$('transaction_check').setValue(data[5].getAttribute('check')=="True"?"Check":null);
		form.scrollTo();
  	}
	
  	function transaction_NewClick(event)
  	{
		$('transaction_edit_error').addClassName("hidden");
		var form = $('teditform');
		form.removeClassName("hidden");
		$('transaction_key').setValue("None");
		$('transaction_ammount').setValue('0');
		$('transaction_text').setValue('New...');
		$('transaction_delete').addClassName("hidden");
		$('transaction_update').innerHTML = "Hinzufügen";
		form.scrollTo();
  	}
	
  	// run some startup code
	document.observe("dom:loaded", function ()
	{  			
		new Ajax.Request("/transactions", {   
			method: 'post',   
			parameters: { action: 'list', project: project_id },
			onSuccess: function(transport) { 
				$('transactions').update(transport.responseText);
				$$('tr.transaction').invoke('observe', 'click', transactionClick);
			}
		} );
		new Ajax.Request("/accounts", {   
			method: 'post',   
			parameters: { action: 'list', project: project_id },
			onSuccess: function(transport) { 
				$('transaction_source').update(transport.responseText); 
				$('transaction_dest').update(transport.responseText); 
			}
		} );
		new Ajax.Request("/currencies", {   
			method: 'post',   
			parameters: { action: 'list', project: project_id },
			onSuccess: function(transport) { 
				$('transaction_currency').update(transport.responseText); 
			}
		} );
	});		
{% endblock %}

{% block body %}
<form id="teditform" enctype="multipart/form-data" class="hidden transaction" onsubmit="return false;">
	<fieldset>
		<div id="transaction_edit_error" class="hidden error">Error message!</div><br/>
		<legend>Transaktion</legend>
		<input id="transaction_key" type="text" class="hidden" readonly="true" size="1" name="KEY" VALUE=""/>
		<input id="project" type="text" class="hidden" readonly="true" size="1" name="PROJECT" VALUE="{{ access_key }}"/>
		<br/>
		
		<label for="transaction_date">Datum</label>
		<input id="transaction_date" type="text" readonly="true" name="TEXT" size="10" VALUE="{% now "Y-m-d" %}"/>
		<input type=button value="..." onclick="displayDatePicker('transaction_date', false, 'ymd', '-');">
		<input type=button value="today" onclick="$('transaction_date').setValue('{% now "Y-m-d" %}');">
		<br/>

					
		<label for="transaction_source">Von</label>
		<select id="transaction_source" class="formInputWidth" name="SOURCE"><option>---</option></select>
		<a href="/accounts?project={{access_key}}">[Konten]</a>
		<br/>
		
		<label for="transaction_dest">An</label>
		<select id="transaction_dest" class="formInputWidth" name="DEST"><option>---</option></select>
		<br/>

		<label for="transaction_ammount">Betrag</label>
		<input id="transaction_ammount" type="text" name="TEXT" size="5" VALUE=""/>
		<select id="transaction_currency" name="CURRENCY"><option>Not loaded</option></select>
		<br/>
						
		<label for="transaction_text">Für was</label>
		<input id="transaction_text" type="text" class="formInputWidth" name="TEXT" size="70" VALUE=""/>
		<br/>

		<label for="transaction_check">Prüfen!</label>
		<input id="transaction_check" type="checkbox" name="CHECK" value="True" />
		<br/>
		
		<button id="transaction_delete" type="submit" name="Action" value="Delete" onclick="transaction_DeleteClick()">Löschen</button>
		<button id="transaction_cancel" type="submit" name="Action" value="Cancel" onclick="transaction_CloseClick()">Abbrechen</button>
		<button id="transaction_update" type="submit" name="Action" value="Update" onclick="transaction_SubmitClick()">Aktualisieren</button>
	</fieldset>
</form>
<button id="transaction_update" type="submit" name="Action" value="Update" onclick="transaction_NewClick()">Neue Transaktion...</button>
<table class="smallFont">
	<thead>
		<tr>
			<th>Datum</th>
			<th>Von</th>
			<th>Nach</th>
			<th>Betrag</th>
			<th>Währung</th>
			<th>Für Was</th>
			<th>Letzte Änderung</th>
		</tr>
	</thead>
	<tbody id="transactions"></tbody>
</table>  
<button id="transaction_update" type="submit" name="Action" value="Update" onclick="transaction_NewClick()">Neue Transaktion...</button>
{% endblock %}
